package com.leetcode.LC;

import java.util.HashMap;
import java.util.HashSet;

public class LC819 {
    public String mostCommonWord(String paragraph, String[] banned) {
        //语句变为小写，去除非字母字符变为空格replace，空格分隔存入Map计数，禁用单词存入Set，在原段落中存在则不存入Map
        paragraph = paragraph.toLowerCase()
                .replace(',', ' ')
                .replace('.', ' ')
                .replace('!', ' ')
                .replace('?', ' ')
                .replace(';', ' ')
                .replace('\'', ' ')
                .replace("  ", " ")
                .replace("   ", " ")
                .replace("    ", " ");

        String[] split = paragraph.split(" ");

        HashSet<String> set = new HashSet<>();
        for (String s : banned) {
            set.add(s);
        }
        HashMap<String, Integer> map = new HashMap<>();
        for (String s : split) {
            //System.out.print(s + " ");
            if (!set.contains(s)) {
                if (map.containsKey(s)) {
                    map.put(s, map.get(s) + 1);
                } else {
                    map.put(s, 1);
                }
            }
        }
        System.out.println();
        int max = 0;
        String rs = "";
        for (String s : map.keySet()) {
            if (map.get(s) > max) {
                max = map.get(s);
                rs = s;
            }
        }
        return rs;
    }

    public static void main(String[] args) {
        System.out.println(new LC819().mostCommonWord("Bob. hIt, baLl", new String[]{"bob", "hit"}));
    }
}
